<?php 
if(!isset($_SESSION)) 
{ 
	session_start(); 
}
ob_start();
include '../dao/connection.php';
include '../content/logsearch.php';
include '../lang.php';
$_SESSION['mlogsearch']=array();
$list["log_ssh.ins_time"]="DESC";
$list["log_ssh.local_time"]="DESC";
$list["log_ssh.version"]="DESC";
$list["log_ssh.status"]="DESC";
$str="log_ssh.ins_time";
$_SESSION['sortn']=$str;
$_SESSION['sort']=$list;
$page=1;
$rows_per_page = $_SESSION['rows_page'] ;
$page_start = ( $page - 1 ) * $rows_per_page; 
$page_end = $page * $rows_per_page;
if(isset($_REQUEST['type'])){
	if($_REQUEST['type']=="searchall"){
		$_SESSION['thucthi']="all";
		$sql="SELECT * from log_ssh  ORDER BY ".$_SESSION['sortn']." ".$_SESSION['sort'][$_SESSION['sortn']];
	}else if($_REQUEST['type']=="searchone"){
		$_SESSION['thucthi']="one";
		$sql="SELECT * from log_ssh where date(log_ssh.ins_time)=date(now()) ORDER BY ".$_SESSION['sortn']." ".$_SESSION['sort'][$_SESSION['sortn']];
	}else if($_REQUEST['type']=="searchtwo"){
		$_SESSION['thucthi']="two";
		$tg=date('Y-m-d',strtotime(date("Y-m-d").'-1 days'));
		$sql="SELECT * from log_ssh where date(log_ssh.ins_time)>='".$tg."' and date(log_ssh.ins_time)<=date(now()) ORDER BY ".$_SESSION['sortn']." ".$_SESSION['sort'][$_SESSION['sortn']];
	}else if($_REQUEST['type']=="searchweek"){
		$_SESSION['thucthi']="week";
		$tg=date('Y-m-d',strtotime(date("Y-m-d").'-1 weeks'));
		$sql="SELECT * from log_ssh where date(log_ssh.ins_time)>'".$tg."' and date(log_ssh.ins_time)<=date(now()) ORDER BY ".$_SESSION['sortn']." ".$_SESSION['sort'][$_SESSION['sortn']];
	}else if($_REQUEST['type']=="searchmonth"){
		$_SESSION['thucthi']="month";
		$tg=date('Y-m-d',strtotime(date("Y-m-d").'-1 months'));
		$sql="SELECT * from log_ssh where date(log_ssh.ins_time)>='".$tg."' and date(log_ssh.ins_time)<=date(now()) ORDER BY ".$_SESSION['sortn']." ".$_SESSION['sort'][$_SESSION['sortn']];
	}
	
	$rs=mysql_query($sql);
	$numrow=mysql_num_rows($rs);
	if($numrow>0){
		$number_of_page = ceil ( $numrow / $rows_per_page ); 
		while($row=mysql_fetch_array($rs)){
			$ob= new logsearch();
			$ob->Instance($row['log_idx'], $row['local_ip'], $row['local_time'], $row['version'], $row['status'], $row['ins_time']);
			$_SESSION['mlogsearch'][]=$ob;
		}
	}
}

?>
			<table style="width:100%;text-align:center;border:1px solid;border-color: black;">
				<tr style="background: #2A4F7E;height:30px;color:white;text-align:center;font-weight:bold;">	
					<td width="50px" style="padding:5px"><?=$l_no?></td>
					<td width="100px"><?=$l_ip?></td>
					<td width="200px"><a onclick="sortdk('sort','<?=$_REQUEST['type']?>',<?=$page?>,'log_ssh.ins_time');" style="cursor:pointer;"><?=$l_servertime?></a>
					<img 
					<?php 
						if($_SESSION['sortn']=='log_ssh.ins_time'){
							if($_SESSION['sort'][$_SESSION['sortn']]=="DESC"){
								?>
								src="images/sd.png" 
								<?php 
							}else{
								?>
								src="images/sa.png" 
								<?php 
							}
						}else{?>
							src="images/sall.png"
							<?php 
						}
					?> style="width:20px;height:20px;" />
					</td>
					<td width="200px"><a onclick="sortdk('sort','<?=$_REQUEST['type']?>',<?=$page?>,'log_ssh.local_time');" style="cursor:pointer;"><?=$l_localtime?></a>
					<img 
					<?php 
						if($_SESSION['sortn']=='log_ssh.local_time'){
							if($_SESSION['sort'][$_SESSION['sortn']]=="DESC"){
								?>
								src="images/sd.png" 
								<?php 
							}else{
								?>
								src="images/sa.png" 
								<?php 
							}
						}else{?>
							src="images/sall.png"
							<?php 
						}
					?> style="width:20px;height:20px;" />
					</td>
					<td width="100px"><a onclick="sortdk('sort','<?=$_REQUEST['type']?>',<?=$page?>,'log_ssh.version');" style="cursor:pointer;"><?=$l_version?></a>
					<img 
					<?php 
						if($_SESSION['sortn']=='log_ssh.version'){
							if($_SESSION['sort'][$_SESSION['sortn']]=="DESC"){
								?>
								src="images/sd.png" 
								<?php 
							}else{
								?>
								src="images/sa.png" 
								<?php 
							}
						}else{?>
							src="images/sall.png"
							<?php 
						}
					?> style="width:20px;height:20px;" />
					</td>
					<td width="100px"><a onclick="sortdk('sort','<?=$_REQUEST['type']?>',<?=$page?>,'log_ssh.status');" style="cursor:pointer;"><?=$l_status?></a>
					<img 
					<?php 
						if($_SESSION['sortn']=='log_ssh.status'){
							if($_SESSION['sort'][$_SESSION['sortn']]=="DESC"){
								?>
								src="images/sd.png" 
								<?php 
							}else{
								?>
								src="images/sa.png" 
								<?php 
							}
						}else{?>
							src="images/sall.png"
							<?php 
						}
					?> style="width:20px;height:20px;" />
					</td>
				</tr>
			<?php 
			if(isset($_SESSION['mlogsearch'])){
				$sl=count($_SESSION['mlogsearch']);
				if($sl>0){
					$slc=$sl;
					if($sl>$page_end){
						$slc=$page_end;
					}
					for ($i=$page_start;$i<$slc;$i++){
						//if ( $i >= $page_start  ){
							$obj= unserialize (serialize ($_SESSION['mlogsearch'][$i]));
							if($i%2==0){
								?>
								<tr class="ctb" style="padding:5px;background: #eef">
								<?php 
							}else{
								?>
								<tr class="ctb" style="padding:5px">
								<?php 
							}
							?>
								<td width="50px" style="text-align:right;padding:5px"><?=($i+1)?></td>
								<td width="100px"><?=$obj->local_ip?></td>
								<td width="200px"><?=$obj->ins_time?></td>
								<td width="200px"><?=$obj->local_time?></td>
								<td width="100px"><?=$obj->version?></td>
								<td width="100px"><?=$obj->status?></td>
							</tr>
							<?php 
						//}
						/*if (($i+1) >= $page_end){ 
							break; 
						}*/
					}
					
					if ( $number_of_page > 1 ){
						?>
						<tr>
							<td colspan="7" style="background:#ccc" >
								<span style="float:left;padding:5px;"><b><?=$l_record?></b> : <b><?=number_format($sl)?></b></span>
								<span style="background:#fff; padding:1px; display: inline-block;">
								<?php 
									if ($page==1)
										$p=2;
									else if ($page==$number_of_page)
										$p=$number_of_page-1;
									else $p=$page;
									if($page!=1){?>
										<a onclick="sortdk('none','<?=$_REQUEST['type']?>',1,'<?=$str?>');" ><span class='button_next'><b><?php echo "<<<";?></b></span> </a>
										<a onclick="sortdk('none','<?=$_REQUEST['type']?>',<?=($page-1)?>,'<?=$str?>');" ><span class='button_next'><b><?php echo " << ";?></b></span> </a>
									<?php  
									}
									for ( $i = $p-1; $i <= $p+1; $i++ ) {
										if ($i<=$number_of_page && $i>0){
											if ($i==$page){
												echo "<span class='button_next' style='color:red'><b>".$i."</b></span>"; 
											}else{
											?>
												<a onclick="sortdk('none','<?=$_REQUEST['type']?>',<?=$i?>,'<?=$str?>');" ><span class="button_next" style="color: #508e21;">
												<?php 
												echo "<b>".$i."</b>";
												?>
												</span> </a>
												<?php 
											}	
										}
									}
									if($page!=$number_of_page){ ?>
										<a onclick="sortdk('none','<?=$_REQUEST['type']?>',<?=($page+1)?>,'<?=$str?>');"><span class='button_next'><b><?php echo ">>";?></b></span> </a>
										<a onclick="sortdk('none','<?=$_REQUEST['type']?>',<?=($number_of_page)?>,'<?=$str?>');" ><span class='button_next'><b><?php echo ">>>";?></b></span> </a>
									<?php 
									}
								?>
								</span>
							</td>
						</tr>
						<?php 
					}
					
				}else{
					?>
					<tr><td colspan="6" style="padding-left: 10px"><?=$l_empty?></td></tr>
					<?php 
				}
			}
			?>
			</table>
<?php mysql_close($connection);?>